package cn.ys.system.service.impl;

import cn.ys.system.domain.Role;
import cn.ys.system.mapper.MenuMapper;
import cn.ys.system.mapper.PermissionMapper;
import cn.ys.system.mapper.RoleMapper;
import cn.ys.system.service.IRoleService;
import cn.ys.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ys
 * @since 2023-01-03
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RoleMapper roleMapper;

    // 新增角色
    @Override
    public void add(Role role) {
        // 新增角色数据
        super.add(role);
        // 新增t_role_menu中间表
        menuMapper.addMiddle(role.getId(),role.getMenus());
        // 添加t_role_permission中间表
        permissionMapper.addMiddle(role.getId(),role.getPermissions());
    }

    // 修改
    @Override
    public void update(Role role) {
        super.update(role);
        // 先删除menu中间表
        menuMapper.deleteByRolesId(role.getId());
        // 再添加menu中间表
        menuMapper.addMiddle(role.getId(),role.getMenus());
        // 先删除t_role_permission中间表
        permissionMapper.deleteByRolesId(role.getId());
        // 再添加t_role_permission中间表
        permissionMapper.addMiddle(role.getId(),role.getPermissions());

    }
}
